<template>
  <el-form :inline="true" @submit.native.prevent>
    <el-form-item>
      <el-select v-model="queryParam.status" clearable
                 placeholder="使用状态" @change="handleQuery">
        <el-option v-for="item in Dic.isShow()"
                   :key="item.value"
                   :label="item.text"
                   :value="item.value"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-select v-model="queryParam.saleUser" placeholder="选择负责人"
                 filterable clearable
                 @change="handleQuery">
        <el-option v-for="item in userList"
                   :key="item.id"
                   :label="item.nickName"
                   :value="item.id"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-input v-model="queryParam.keyword"
                @keyup.enter="handleQuery"
                placeholder="请输入名称"
                type="text"
                prefix-icon="search"
                auto-complete="off"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="openRow">新增</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData.list" v-loading="loading" border height="calc(100vh - 210px)">
    <el-table-column label="标题" prop="title" min-width="300">
      <template v-slot="{row:{title,id}}">
        <el-button link type="info" @click="copy(Dic.OldUrl(id))">
          <span v-text="title"/>
          <el-icon class="el-icon--right">
            <CopyDocument/>
          </el-icon>
        </el-button>
      </template>
    </el-table-column>
    <el-table-column label="文件" prop="file" width="80" align="center">
      <template v-slot="{row:{rFile2,file}}">
        <el-button link type="success" v-if="file" :disabled="!file" @click="openPicture(file)">
          预览
        </el-button>
        <el-button link type="warning" v-else :disabled="!rFile2" @click="openPictureOld(rFile2)">
          预览
        </el-button>
      </template>
    </el-table-column>
    <el-table-column label="负责人" prop="saleName" min-width="140" align="center"/>
    <el-table-column label="更新人" prop="operatorName" min-width="140" align="center"/>
    <el-table-column label="备注" prop="remark" min-width="140" align="center"/>
    <el-table-column label="排序" prop="sort" width="140" align="center"/>
    <el-table-column label="状态" prop="status" width="100" align="center" :formatter="Dic.isShow"/>
    <el-table-column label="更新时间" prop="updateTime" width="180" align="center"/>
    <el-table-column label="操作" fixed="right" width="120" align="center">
      <template v-slot="{row}">
        <el-button link type="success" @click="openRow(row)">
          编辑
        </el-button>
        <el-button link type="info" @click="openLogs(row)">
          记录
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination layout="->,total,sizes,prev,pager,next,jumper"
                 v-model:current-page="queryParam.page"
                 v-model:page-size="queryParam.limit"
                 :total="tableData.total" background
                 @current-change="loadList"
                 @size-change="loadList"/>
  <el-image-viewer show-progress
                   v-if="showPreview"
                   :url-list="[currentUrl]"
                   @close="showPreview = false"/>
  <expo-list-detail v-model="openRowDialog"
                    :info="currentRow"
                    :refresh="loadList"
                    :close="()=>{this.openRowDialog = false}"/>
  <expo-list-logs v-model="openLogsDialog"
                  :info="currentRow"
                  :refresh="loadList"
                  :close="()=>{this.openLogsDialog = false}"/>
</template>

<script lang="ts">
import expoApi from "@/api/expo";
import expoListDetail from "@/components/expo/expoListDetail.vue";
import expoListLogs from "@/components/expo/expoListLogs.vue";
import Dic from "@/utils/dic"
import userApi from "@/api/user";
import {CopyDocument} from "@element-plus/icons-vue";
import {ElNotification} from "element-plus";

export default {
  name: "expoList",
  components: {expoListDetail, expoListLogs, CopyDocument},
  data() {
    return {
      Dic,
      userList: [],
      queryParam: {
        page: 1,
        limit: 20,
        keyword: "",
        byLan: true,
        status: 1
      },
      tableData: {
        list: [],
        total: 0,
      },
      openRowDialog: false,
      openLogsDialog: false,
      currentRow: {},
      loading: false,
      showPreview: false,
      currentUrl: ""
    };
  },
  created() {
    this.loadSeller()
    this.loadList();
  },
  methods: {
    handleQuery() {
      this.queryParam.page = 1;
      this.loadList();
    },
    loadList() {
      this.loading = true
      expoApi.expoList(this.queryParam).then(res => {
        this.loading = false
        this.tableData = res.data
      })
    },
    openRow(row) {
      this.openRowDialog = true;
      this.currentRow = row
    },
    openLogs(row) {
      this.openLogsDialog = true;
      this.currentRow = row
    },
    loadSeller() {
      userApi.sellerList().then(res => this.userList = res.data)
    },
    openPictureOld(value) {
      this.showPreview = true;
      this.currentUrl = Dic.OldFile(value)
    },
    openPicture(value) {
      this.showPreview = true;
      this.currentUrl = Dic.imgUrl(value)
    },
    copy(url) {
      navigator.clipboard.writeText(url);
      ElNotification.success({
        title: '复制成功',
        message: url,
      })
    }
  }
}
</script>
<style lang="scss" scoped>

</style>